# -*- coding: utf-8 -*-
# @Time : 2022-01-15 16:32
# @Author : Z
# @Function :
import re
import json
import requests

# session管理，自动关联set-cookie的内容，加快服务器的连接速度
session = requests.session()
# session= requests.sessions.Session()

# 不在session里面的字段
# session.headers['X-Powered-B'] = 'PHP/7.0.33'
# session.cookies['aa'] = 'bb'
#
# # application/x-www-form-rulencoded,表单数据参数，requests传参的时候使用字典，data=
# params = {
#     'username': '13800138006',
#     'password': '123456',
#     "verify_code": "1111"
# }
#
# r = session.post('http://testingedu.com.cn:8000/index.php?m=Home&c=User&a=do_login&t=0.19765239601291884',
#                  data=params)

# print(r)
# # 打印状态码
# print(r.status_code)
# # 打印文本
# print(r.text)
# # json结果
# print(r.json())
# # 返回请求头
# print(r.headers)


# 指定请求内容类型
session.headers['Content-Type'] = 'application/x-www-form-urlencoded;charset=UTF-8'

# 获取token
r = session.post('http://testingedu.com.cn:8081/inter/HTTP/auth')
# 设置返回结果的编码
text_r = r.content.decode('utf-8')
# 二进制编码
# print(r.content.decode('unicode-escape'))
# 将返回的结果转化为字典，如果出现异常，就返回个空字典
try:
    json_r = json.loads(text_r)
except:
    json_r = {}

print("请求头信息："+str(session.headers))
# 通过键提取字典里面对应的值
print("提取token：" + json_r.get('token'))

# 添加token到header
session.headers['token'] = json_r.get('token')
# 指定请求内容类型
session.headers['Content-Type'] = 'application/x-www-form-urlencoded;charset=UTF-8'

# application/x-www-form-rulencoded,表单数据参数，requests传参的时候使用字典，data=
params = {"username": "Will", "password": "123456"}

# 登陆
r = session.post('http://testingedu.com.cn:8081/inter/HTTP/login', data=params)
print('登陆成功的请求头信息：'+str(session.headers))
print("登陆成功的传参内容："+str(session.params))
print("登陆成功的返回结果：" + r.text)
print("登陆成功的返回头信息：" + str(r.headers))

# 登出，后置处理，注销，消除该用例对后续操作的影响
r = session.post('http://testingedu.com.cn:8081/inter/HTTP/logout')
print("登陆成功的传参内容："+str(session.params))
print('退出登录的返回结果：' + r.text)

# help(requests.session())

# # 字典传参
# payload = dict(key1='value1', key2='value2')
# print(type(payload))
# r = requests.post('https://httpbin.org/post', data=payload)
# print(r.text)
# a = dict(a='1',b='2')
# print(type(a))

# mock测试
# r = requests.get('http://127.0.0.1:9000/get_user')
# res = r.text
# print(res)
# print(type(res))
# # 将返回的json字符串结果转换成字典
# res_dit = json.loads(res)
# print(res_dit)
# print(res_dit[0].get('id'))

import requests
from unittest import mock

# def request_baidu():
#     resp = requests.get('http://www.baidu.com')
#     return resp.status_code


# # 模拟调用request_baidu，且返回值是500
# request_baidu = mock.Mock(return_value=500)
# print(request_baidu())
# #打印是否被调用
# print(request_baidu.called)
# # 打印调用次数
# print(request_baidu.call_count)

# url = r"http://127.0.0.1/api/app/"
# param = {
#     "applicationTypeId": 0,
#     "description": "",
#     "entranceUrl": "",
#     "isWindow": 0,
#     "logoUrl": "",
#     "name": "bb55",
#     "router": "",
#     "sort": 0,
#     "systemId": 0,
#
# }
# return_content = {
#     "code": 0,
#     "data": 0,
#     "msg": "",
#     "success": "true"
# }
# request = mock.Mock(return_value=return_content)
# response = request(url=url, method="post", data=param)
# print(response)


